package com.socialbank.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wqdata.auth.bean.Dept;
import com.wqdata.auth.bean.Role;
import com.wqdata.auth.bean.RoleDept;
import com.wqdata.auth.bean.RoleMenu;
import com.wqdata.auth.dto.TreeDto;
import com.wqdata.auth.service.DeptService;
import com.wqdata.auth.service.MenuService;
import com.wqdata.auth.service.RoleService;
import com.wqdata.auth.util.PageParam;
import com.wqdata.auth.util.PageUtils;
import com.wqdata.auth.vo.RoleVo;

@Controller
@RequestMapping("sys/role")
public class RoleController extends BaseController{

	private static Logger logger = Logger.getLogger(RoleController.class);

	@Autowired
	private RoleService roleService;

	@Autowired
	private MenuService menuService;
	
	@Autowired
	private DeptService deptService;

	@RequestMapping("toRoleList")
	public String toRole(HttpServletRequest request) {
	    //System.out.println(request.getSession().getAttribute("org.apache.shiro.subject.support.DefaultSubjectContext_PRINCIPALS_SESSION_KEY"));
		return "systemConfig/RoleList";
	}

	// 分页查询用户列表
	@RequestMapping("/getRoleList")
	public @ResponseBody Map<String, Object> getRoleList(PageParam pageParam) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Role role = new Role();
		// 条件注入部分
		List<Role> roleList = new ArrayList<>();
		PageInfo<Role> pageInfo = roleService.getRoleListPage(role, pageParam);
		roleList = pageInfo.getList();
		resultMap.put("roleList", roleList);
		// 获取分页条的java代码
		String pageStr = PageUtils.pageStr(pageInfo, "roleMgr.getRoleListPage");
		resultMap.put("pageStr", pageStr);
		return resultMap;
	}

	@RequestMapping("toRoleEdit")
	@RequiresPermissions(value = { "role:add", "role:update" }, logical = Logical.OR)
	public String toRoleEdit(@ModelAttribute("editFlag") int editFlag, Long roleId, Model model) {
		List<TreeDto> TreeMenuList = menuService.getAllMenuDto();
		model.addAttribute("treeMenuList", TreeMenuList);
		List<Dept> treeDeptList = deptService.getAllDeptDto();
		model.addAttribute("treeDeptList", treeDeptList);
		
		// 1 为增加 2为修改
		if (editFlag == 2) {
			Role role = roleService.getRoleById(roleId);
			model.addAttribute("role", role);
			List<RoleMenu> roleMenus = roleService.getRoleMenuListByRoleId(roleId);
			model.addAttribute("roleMenus", roleMenus);
			List<RoleDept> roleDepts = roleService.getRoleDeptListByRoleId(roleId);
			model.addAttribute("roleDepts", roleDepts);
		}
		return "systemConfig/RoleAdd";
	}

	@RequestMapping(value = "saveRole", method = RequestMethod.POST)
	@RequiresPermissions("role:add")
	public @ResponseBody Map<String, Object> saveRole(@RequestBody RoleVo roleVo) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			roleService.addRoleVo(roleVo);
			resultMap.put("code", 200);
			resultMap.put("message", "增加用户信息成功");
		} catch (Exception e) {
			logger.error("操作用户失败", e);
		}
		return resultMap;
	}

	@RequestMapping(value = "updateRole", method = RequestMethod.POST)
	@RequiresPermissions("role:update")
	public @ResponseBody Map<String, Object> updateRole(@RequestBody RoleVo roleVo) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			roleService.updateRoleVo(roleVo);
			resultMap.put("code", 200);
			resultMap.put("message", "修改用户信息成功");
		} catch (Exception e) {
			logger.error("操作用户失败", e);
		}
		return resultMap;
	}

}
